package com.example.day2_menu;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


    private TextView tvContext;
    private TextView tv;
    private TextView tvwindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //长按，弹出上下文菜单
        registerForContextMenu(tvContext);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showpopupMenu();
            }
        });
        tvwindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showWindow();
            }
        });
    }

    private void showWindow() {
        //实例化popupWindow
        PopupWindow popupWindow = new PopupWindow();
        View inflate = LayoutInflater.from(this).inflate(R.layout.layout_window, null);
        TextView cancel = inflate.findViewById(R.id.cancel);
        TextView tu = inflate.findViewById(R.id.tu);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
                //窗体消失
                popupWindow.dismiss();
            }
        });
        tu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "点击了图库", Toast.LENGTH_SHORT).show();
                //窗体消失
                popupWindow.dismiss();
            }
        });
        //背景半透明
        //获取窗体的属性
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha = 0.5f;//透明度 浮点类型 0-1
        getWindow().setAttributes(attributes);
        //窗口消失恢复原来的状态
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                attributes.alpha = 1f;//透明度 浮点类型 0-1
                getWindow().setAttributes(attributes);
            }
        });
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_window));
        //设置四个必要属性
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);//宽度为屏幕宽度
        popupWindow.setHeight(300);//高度  也可以给出明确的数字值，同样可以参考宽度给一个值
        popupWindow.setContentView(inflate);//要显示的内容  自定义的布局
        popupWindow.setOutsideTouchable(true);//点击外部，窗体会消失
        popupWindow.showAtLocation(tvwindow, Gravity.BOTTOM,0,0);//常用
        //popupWindow.showAsDropDown(tvwindow,0,0);
    }

    private void showpopupMenu() {
        //实例化 popupMenu
        PopupMenu popupMenu = new PopupMenu(this, tv);
        //加载资源
        popupMenu.inflate(R.menu.option_menu);
        //点击菜单
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.item1:
                        Toast.makeText(MainActivity.this, "点击了添加好友菜单", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item2:
                        Toast.makeText(MainActivity.this, "点击了扫一扫菜单", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item3:
                        Toast.makeText(MainActivity.this, "点击了二维码菜单", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item31:
                        Toast.makeText(MainActivity.this, "点击了二维码1菜单", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item32:
                        Toast.makeText(MainActivity.this, "点击了二维码2菜单", Toast.LENGTH_SHORT).show();
                        break;

                }
                return false;
            }
        });
        //显示菜单
        popupMenu.show();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //加载资源文件---菜单
        getMenuInflater().inflate(R.menu.option_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item1:
                Toast.makeText(this, "点击了添加好友菜单", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item2:
                Toast.makeText(this, "点击了扫一扫菜单", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item3:
                Toast.makeText(this, "点击了二维码菜单", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item31:
                Toast.makeText(this, "点击了二维码1菜单", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item32:
                Toast.makeText(this, "点击了二维码2菜单", Toast.LENGTH_SHORT).show();
                break;

        }
        return super.onOptionsItemSelected(item);
    }
    //上下文菜单

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.option_menu, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item1:
                Toast.makeText(this, "点击了添加好友菜单", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item2:
                Toast.makeText(this, "点击了扫一扫菜单", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item3:
                Toast.makeText(this, "点击了二维码菜单", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item31:
                Toast.makeText(this, "点击了二维码1菜单", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item32:
                Toast.makeText(this, "点击了二维码2菜单", Toast.LENGTH_SHORT).show();
                break;

        }
        return super.onContextItemSelected(item);
    }

    private void initView() {
        tvContext = (TextView) findViewById(R.id.tv_context);
        tv = (TextView) findViewById(R.id.tv);
        tvwindow = (TextView) findViewById(R.id.tvwindow);
    }
}